package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.ShowTypeListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/1.
 */
public class ShowTabListAdapter extends ArrayAdapter<ShowTypeListGet.ShowTypeEntity> {

    private LayoutInflater layoutInflater;
    private Context context;
    public HashMap<Integer, Boolean> tabChooseMap;

    public ShowTabListAdapter(Context context, List<ShowTypeListGet.ShowTypeEntity> objects) {

        super(context, 0, objects);

        this.context = context;

        layoutInflater = ((Activity)context).getLayoutInflater();

        tabChooseMap = new HashMap<>();

    }

    public void notifyDataSetChanged(List<ShowTypeListGet.ShowTypeEntity> objects, int pos) {

        for(int i=0;i<objects.size();i++){

            if(i==pos){

                tabChooseMap.put(i, true);

            }else {

                tabChooseMap.put(i, false);

            }

        }

        notifyDataSetChanged();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        ShowTypeListGet.ShowTypeEntity showTypeEntity = getItem(position);

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_show_tab_list, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if(tabChooseMap.get(position)){

            viewHolder.item_show_tab_tv.setTextColor(ContextCompat.getColor(context, R.color.theme_color));

        }else{

            viewHolder.item_show_tab_tv.setTextColor(ContextCompat.getColor(context, R.color.text_color));

        }

        viewHolder.item_show_tab_tv.setText(showTypeEntity.title);

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.item_show_tab_tv) public TextView item_show_tab_tv;

    }

}
